/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.j002.requirementengineeringmuni.impl;

import cz.muni.fi.j002.requirementengineeringmuni.entities.Card;
import cz.muni.fi.j002.requirementengineeringmuni.entities.Card.Sign;
import cz.muni.fi.j002.requirementengineeringmuni.entities.Card.Value;
import cz.muni.fi.j002.requirementengineeringmuni.Deck;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author stefan
 */
public class DeckImpl implements Deck {
    
    private Set<Card> remainingCards;
    private Set<Card> drawnCards;
    
    public DeckImpl() {
        this.remainingCards = createNewSetOfCards();
        this.drawnCards = new HashSet<Card>();
    }
    
    @Override
    public Card drawCard() {
        
        if(!this.hasCardsLeft()) {
            throw new IllegalStateException("There are no more cards to be drawn from remaining cards set");
        }
        
        // take random Card
        int cardIndex = new Random().nextInt(remainingCards.size());
        
        int i = 0;
        for(Card card : remainingCards) {
            if(i == cardIndex) {
                remainingCards.remove(card);
                drawnCards.add(card);
                return card;
            }
            i++;
        }
        
        return null; // this should never happen
    }
    
    @Override
    public boolean hasCardsLeft() {
        return !remainingCards.isEmpty();
    }
    
    @Override
    public Set<Card> getDrawnCards() {
        return Collections.unmodifiableSet(drawnCards);
    }
    
    public static Set<Card> createNewSetOfCards() {
        Set<Card> retval = new HashSet<Card>();
        
        for(Sign sign : Sign.values()) {
            for(Value value : Value.values()) {
                retval.add(new Card(sign, value));
            }
        }
        
        return  retval;
    }

    @Override
    public Set<Card> getRemainingCards() {
        return Collections.unmodifiableSet(remainingCards);
    }
}
